package com.hsnn.medstgmini.base.std.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.annotations.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfoDetail;
import com.hsnn.medstgmini.base.std.model.StdCompinfoDetailLog;
import com.hsnn.medstgmini.base.std.model.StdCompinfoLog;
import com.hsnn.medstgmini.base.std.service.StdCompinfoDetailManager;
import com.hsnn.medstgmini.base.std.service.StdCompinfoLogManager;
import com.hsnn.medstgmini.base.std.service.StdCompinfoManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * 企业库 维护记录 控制层
 * 
 * @author hss@njhsnn 2015年6月9日
 */
@Controller
@RequestMapping("/stdCompinfoLog")
public class StdCompinfoLogController {
	@Source
	private StdCompinfoLogManager stdCompinfoLogManager;

	@Source
	private StdCompinfoManager stdCompinfoManager;
	
	@Source
	private StdCompinfoDetailManager stdCompinfoDetailManager;

	@Autowired
	public void setStdCompinfoLogManager(
			StdCompinfoLogManager stdCompinfoLogManager) {
		this.stdCompinfoLogManager = stdCompinfoLogManager;
	}

	@Autowired
	public void setStdCompinfoManager(StdCompinfoManager stdCompinfoManager) {
		this.stdCompinfoManager = stdCompinfoManager;
	}

	@Autowired
	public void setStdCompinfoDetailManager(
			StdCompinfoDetailManager stdCompinfoDetailManager) {
		this.stdCompinfoDetailManager = stdCompinfoDetailManager;
	}
	
	@Autowired
	private HttpServletRequest request;
	
	private static final Logger log = Logger.getLogger(StdCompinfoController.class);

	/**
	 * 企业库 记录维护增加
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/stdCompinfoLogAdd", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String stdCompinfoLogAdd(HttpServletRequest request,
			HttpServletResponse response) {
		printParams();

		StdCompinfoLog stdCompinfoLog = new StdCompinfoLog();// 记录维护实体类
		StdCompinfoDetailLog stdCompinfoDetailLog  = new StdCompinfoDetailLog();//详情日志实体类
		StdCompinfoDetail stdCompinfoDetail = new StdCompinfoDetail();
		String Id = request.getParameter("compIdStatus");
		String[] strId = Id.split(",");
		Date date = new Date();
		SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = fomat.format(date);
		
		String compId = "";// 企业库id
		String compName = "";// 企业名称
		String compCode = "";// 企业编码
		
		String compSpelCode = "";// 企业名称拼音码
		String compNameEng = "";// 企业英文名称
		String compShortName = "";// 企业简称
		String compShortSpelCode = "";// 简称拼音码
		String areaId = "";// 注册地行政区划代码
		String compRegId = "";// 企业注册类型
		String orgCode = "";// 组织机构代码
		String compGroupId = "";// 所属集团ID
		String compPhone = "";// 机构电话
		short compClassify = (short) 0;//企业分类 0默认 1药品2耗材 3药品耗材
		String historyCode = "";//历史企业编码
		short mergeState =  (short) 0;//合并处理状态 0未处理1已处理
		String compAddress = "";// 机构地址
		short compType = (short) 0;// 企业类型（企业属性）1.生产企业2.经营配送企业
		short status = (short) 0;// 状态
		String updUser = "";
		
		
		int strlong = strId.length;
		for (int j = 0; j < strlong; j++) {
			List<StdCompinfo> list = stdCompinfoManager
					.queryStdCompinfoById(strId[j]);
			for (int i = 0; i < strlong;) {
				compId = ((StdCompinfo) list.get(0)).getCompId();
				compName = ((StdCompinfo) list.get(0)).getCompName();
				compCode = ((StdCompinfo) list.get(0)).getCompCode();
				 compSpelCode = ((StdCompinfo) list.get(0)).getCompCode();// 企业名称拼音码
				 compNameEng = ((StdCompinfo) list.get(0)).getCompNameEng();// 企业英文名称
				 compShortName = ((StdCompinfo) list.get(0)).getCompShortName();// 企业简称
				 compShortSpelCode = ((StdCompinfo) list.get(0)).getCompShortSpelCode();// 简称拼音码
				 areaId = ((StdCompinfo) list.get(0)).getAreaId();// 注册地行政区划代码
				 compRegId = ((StdCompinfo) list.get(0)).getCompRegId();// 企业注册类型
				 orgCode = ((StdCompinfo) list.get(0)).getOrgCode();// 组织机构代码
				 compGroupId = ((StdCompinfo) list.get(0)).getCompGroupId();// 所属集团ID
				 compPhone = ((StdCompinfo) list.get(0)).getCompPhone();// 机构电话
				 compClassify =((StdCompinfo) list.get(0)).getCompClassify();//企业分类 0默认 1药品2耗材 3药品耗材
				 historyCode = ((StdCompinfo) list.get(0)).getHistoryCode();//历史企业编码
				 mergeState = ((StdCompinfo) list.get(0)).getMergeState();//合并处理状态 0未处理1已处理
				 compAddress = ((StdCompinfo) list.get(0)).getCompAddress();// 机构地址
				
				compType = (short) ((StdCompinfo) list.get(0)).getCompType();
				status = (short) ((StdCompinfo) list.get(0)).getStatus();
				updUser = ((StdCompinfo) list.get(0)).getUpdUser();
				stdCompinfoDetail = ((StdCompinfo) list.get(0)).getStdCompinfoDetail();

				stdCompinfoLog.setCompId(compId);// 当前行到到id
				stdCompinfoLog.setCompName(compName);// 规格名称
				stdCompinfoLog.setCompCode(compCode);// 规格编码
				stdCompinfoLog.setCompSpelCode(compSpelCode);
				stdCompinfoLog.setCompNameEng(compNameEng);
				stdCompinfoLog.setCompShortName(compShortName);
				stdCompinfoLog.setCompShortSpelCode(compShortSpelCode);
				stdCompinfoLog.setAreaId(areaId);
				stdCompinfoLog.setCompRegId(compRegId);
				stdCompinfoLog.setOrgCode(orgCode);
				stdCompinfoLog.setCompGroupId(compGroupId);
				stdCompinfoLog.setCompPhone(compPhone);
				stdCompinfoLog.setHistoryCode(historyCode);
				stdCompinfoLog.setCompAddress(compAddress);
				stdCompinfoLog.setUpdDatetime(Timestamp.valueOf(strDate));// 更新时间
				stdCompinfoLog.setCompClassify(compClassify);
				stdCompinfoLog.setMergeState(mergeState);
				stdCompinfoLog.setUpdUser(updUser);// 更新人
				stdCompinfoLog.setCompType(compType);// 企业类型
				stdCompinfoLog.setStatus(status);// 状态

				stdCompinfoLogManager.add(stdCompinfoLog);
				
				if(true){
					stdCompinfoDetailLog.setLogId(stdCompinfoLog.getLogId());
					stdCompinfoDetailLog.setCompId(stdCompinfoDetail.getCompId());
					stdCompinfoDetailLog.setRegCap(stdCompinfoDetail.getRegCap());// 企业注册资本
					stdCompinfoDetailLog.setCompProfile(stdCompinfoDetail.getCompProfile());// 企业简称
					stdCompinfoDetailLog.setBizScope(stdCompinfoDetail.getBizScope());
					stdCompinfoDetailLog.setLegalRepName(stdCompinfoDetail.getLegalRepIdnum());
					stdCompinfoDetailLog.setLegalRepIdnum(stdCompinfoDetail.getLegalRepIdnum());
					stdCompinfoDetailLog.setLegalRepTelephone(stdCompinfoDetail.getLegalRepTelephone());
					stdCompinfoDetailLog.setLegalRepHandphone(stdCompinfoDetail.getLegalRepHandphone());
					stdCompinfoDetailLog.setContactName(stdCompinfoDetail.getContactName());
					stdCompinfoDetailLog.setContactIdnum(stdCompinfoDetail.getContactIdnum());
					stdCompinfoDetailLog.setContactPos(stdCompinfoDetail.getContactPos());
					stdCompinfoDetailLog.setContactTelephone(stdCompinfoDetail.getContactTelephone());
					stdCompinfoDetailLog.setContactHandphone(stdCompinfoDetail.getContactHandphone());
					stdCompinfoDetailLog.setContactEmail(stdCompinfoDetail.getContactEmail());
					stdCompinfoDetailLog.setContactQq(stdCompinfoDetail.getContactQq());
					stdCompinfoDetailLog.setBizLiceNo(stdCompinfoDetail.getBizLiceNo());
					stdCompinfoDetailLog.setBizLiceStartDate(stdCompinfoDetail.getBizLiceStartDate());
					stdCompinfoDetailLog.setBizLiceEndDate(stdCompinfoDetail.getBizLiceEndDate());
					stdCompinfoDetailLog.setTaxCode(stdCompinfoDetail.getTaxCode());
					//stdCompinfoDetailLog.setIndusCode(stdCompinfoDetail.getIndusCode());
					stdCompinfoDetailLog.setIndusCodeStartDate(stdCompinfoDetail.getIndusCodeStartDate());
					stdCompinfoDetailLog.setIndusCodeEndDate(stdCompinfoDetail.getIndusCodeEndDate());
					//stdCompinfoDetailLog.setLegalRepCertCode(stdCompinfoDetail.getLegalRepCertCode());
					stdCompinfoDetailLog.setLegalRepStartDate(stdCompinfoDetail.getLegalRepStartDate());
					stdCompinfoDetailLog.setLegalRepEndDate(stdCompinfoDetail.getLegalRepEndDate());
					stdCompinfoDetailLog.setUpdDatetime(stdCompinfoDetail.getUpdDatetime());
					stdCompinfoDetailLog.setUpdUser(stdCompinfoDetail.getUpdUser());
					
					stdCompinfoDetailManager.add(stdCompinfoDetailLog);
				}
				break;
			}
		}

		return "redirect:/stdCompinfo/stdCompinfoListJsp.html";
	}

	/**
	 * 企业库 查询所有数据
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/stdCompinfoLogDetail", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String stdCompinfoLogDetail(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		printParams();
		String compId = request.getParameter("compId");
		request.setAttribute("compId", compId);
		request.setAttribute("compGroupId", request.getParameter("compGroupId"));
		request.setAttribute("compCode", request.getParameter("compCode"));
		String compName = new String(request.getParameter("compName").getBytes("iso8859-1"),"utf-8");
		request.setAttribute("compName", compName);
		return "/base/std/compHis";// 跳转到详情页面
	}

	/**
	 * 企业库 查询及条件查询
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/stdCompinfoLogList")
	@ResponseBody
	@JsonIgnore
	public Pagination getstdCompinfoLogList(HttpServletRequest request,String date1,String date2) {
		printParams();
		Pagination page = new Pagination(request);
		StdCompinfoLog stdCompinfoLog = new StdCompinfoLog();

		try {
			String compId = request.getParameter("compId");
			stdCompinfoLog.setCompId(compId);
			stdCompinfoLog.setDate1(date1);
			stdCompinfoLog.setDate2(date2);
			String status = request.getParameter("status");
			if(StringUtils.isNotEmpty(status)){
				stdCompinfoLog.setStatus(Short.valueOf(status));
			}
			List list = stdCompinfoLogManager
					.queryStdCompinfoLogByName(page,stdCompinfoLog);
			page.setRecords(stdCompinfoLog.getCount());
			page.setRows(list);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
}
